// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MessageBox.java

package ui;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

// Referenced classes of package ui:
//            BrowseCanvas, StringUtil, PageListener

public class MessageBox
    implements Runnable
{

    public MessageBox(String caption, int width, int type, BrowseCanvas canvas, PageListener listener)
    {
        this.type = 0;
        n = new int[2];
        height = 0;
        this.canvas = canvas;
        font = BrowseCanvas.FONTSMALL;
        font_height = font.getHeight();
        if(caption != null)
        {
            captions = StringUtil.getArrayFitString(font, caption, width);
            height = captions.length * font_height + 4;
        }
        this.type = type;
        if(bar_size > 0)
            height += 12;
        anchor = (width * 2) / 3;
        this.width = width + 4;
        this.listener = listener;
        n[0] = -anchor;
        n[1] = n[0] * 2 - 30;
        isrun = false;
        animated = false;
    }

    public void draw(Graphics g, int swidth, int bottom)
    {
        int x = (swidth >> 1) - (width >> 1);
        int y = bottom - height - 2;
        g.setClip(x, y, width + 1, height + 1);
        g.setColor(0xffffff);
        g.drawLine(x, y, x + width, y);
        g.drawLine(x, y, x, y + height);
        g.setColor(0x808080);
        g.drawLine(x, y + height, x + width, y + height);
        g.drawLine(x + width, y, x + width, y + height);
        g.setColor(0xcecac2);
        g.fillRect(x + 1, y + 1, width - 1, height - 1);
        g.setColor(0);
        if(captions != null)
        {
            g.setFont(font);
            x = swidth >> 1;
            y += 2;
            for(int i = 0; i < captions.length; i++)
            {
                g.drawString(captions[i], x, y, 17);
                y += font_height;
            }

        }
        if(isrun)
        {
            x = ((swidth >> 1) - (width >> 1)) + 2;
            g.setColor(0x362f2d);
            g.setClip(x, y, width - 2, 2);
            for(int i = 0; i < 2; i++)
            {
                int y1 = y;
                int x1 = n[i] >= 0 ? n[i] : 0;
                x1 += x;
                int w = n[i] >= 0 ? anchor : anchor + n[i];
                if(w >= 0)
                {
                    g.setColor(0xffffff);
                    g.fillRect(x1, y1, w, 1);
                    g.setColor(0x6c5f3b);
                    y1++;
                    g.fillRect(x1, y1, w, 5);
                    g.setColor(0x56471e);
                    y1 += 5;
                    g.fillRect(x1, y1, w, 2);
                    g.setColor(0x463609);
                    y1 += 2;
                    g.fillRect(x1, y1, w, 2);
                }
            }

        } else
        if(bar_size > 0)
        {
            int w = width - 4;
            x = ((swidth >> 1) - (width >> 1)) + 2;
            g.setColor(0x362f2d);
            g.fillRect(x, y, w, 10);
            g.setColor(0xa39b84);
            w = (w * bar_progress) / bar_size;
            g.fillRect(x, y, w, 4);
            g.setColor(0x867b5e);
            g.fillRect(x, y + 4, w, 2);
            g.setColor(0x56c5f3b);
            g.fillRect(x, y + 6, w, 2);
            g.setColor(0x56471e);
            g.fillRect(x, y + 8, w, 2);
        }
    }

    public void setBarSize(int s)
    {
        bar_size = s;
        bar_progress = 0;
        canvas.redraw(8);
    }

    public void setBarProgress(int s)
    {
        if(s > bar_size)
            bar_progress = bar_size;
        else
            bar_progress = s;
        canvas.redraw(8);
    }

    public boolean keyPressed(int keyCode)
    {
        if(keyCode == BrowseCanvas.KEYSOFTRIGHT)
        {
            if((type & 2) == 2)
            {
                if(listener != null)
                    listener.onMessageBox(2);
                canvas.redraw(3);
                return true;
            }
        } else
        if(keyCode == BrowseCanvas.KEYSOFTLEFT && (type & 1) == 1)
        {
            if(listener != null)
                listener.onMessageBox(1);
            canvas.redraw(3);
            return true;
        }
        return false;
    }

    public void start()
    {
        isrun = true;
        (new Thread(this)).start();
        bar_size = 50;
        bar_progress = 0;
    }

    public void stop()
    {
        isrun = false;
    }

    public void run()
    {
        while(isrun) 
        {
            try
            {
                Thread.sleep(250L);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            for(int i = 0; i < 2; i++)
            {
                n[i] += 10;
                if(n[i] >= width - 4)
                    n[i] = n[1 - i] - anchor - 30;
            }

            canvas.redraw(8);
        }
    }

    public static final int OK = 1;
    public static final int CANCEL = 2;
    public static final int OK_CANCEL = 3;
    private static final int HEIGHT_BAR = 2;
    public String captions[];
    private int bar_size;
    private int bar_progress;
    public int width;
    public int height;
    public int type;
    private Font font;
    private int font_height;
    private BrowseCanvas canvas;
    private PageListener listener;
    private boolean isrun;
    private int n[];
    private int anchor;
    boolean animated;
}
